package com.sc.nft.dto;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sc.nft.entity.EcologyHatchConf;
import com.sc.nft.entity.EcologyHatchMethodCollectionConf;
import com.sc.nft.entity.EcologyHatchRecordsRewards;
import com.sc.nft.entity.vo.ecology.HatchRecordsRewardsDetailsVO;
import com.sc.nft.enums.EcologyRewardsType;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.entity.EcologyHatchRewardsConf;
import com.sc.nft.mapper.EcologyHatchRewardsConfMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 生态-孵化-奖励配置表(sc_ecology_hatch_rewards_conf)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-10-13 17:22:11
 */
@Slf4j
@Repository
public class EcologyHatchRewardsConfDao extends ServiceImpl<EcologyHatchRewardsConfMapper, EcologyHatchRewardsConf> {

    public List<EcologyHatchRewardsConf> getListByEcologyHatchId(Long ecologyHatchId, Long methodId) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        wrapper.eq(EcologyHatchRewardsConf::getMethodId, methodId);
        return list(wrapper);
    }

    public List<EcologyHatchRewardsConf> getListByEcologyHatchId(Long ecologyHatchId) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        return list(wrapper);
    }
    public List<Long> getPropsIdsByEcologyHatchId(Long ecologyHatchId) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(EcologyHatchRewardsConf::getLinkId);
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        wrapper.eq(EcologyHatchRewardsConf::getRewardsType,EcologyRewardsType.EQUITY_PROPS);
        return listObjs(wrapper,(x)->(Long) x);
    }


    public boolean updateSmeltingRewardsConf(EcologyHatchRewardsConf ecologyHatchRewardsConf) {
        LambdaUpdateWrapper<EcologyHatchRewardsConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getRewardsCycle()) && ecologyHatchRewardsConf.getRewardsCycle() > 0), EcologyHatchRewardsConf::getRewardsCycle, ecologyHatchRewardsConf.getRewardsCycle());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getRewardsType())), EcologyHatchRewardsConf::getRewardsType, ecologyHatchRewardsConf.getRewardsType());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getLinkName())), EcologyHatchRewardsConf::getLinkName, ecologyHatchRewardsConf.getLinkName());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getLinkImage())), EcologyHatchRewardsConf::getLinkImage, ecologyHatchRewardsConf.getLinkImage());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getLinkId()) && ecologyHatchRewardsConf.getLinkId().intValue() > 0), EcologyHatchRewardsConf::getLinkId, ecologyHatchRewardsConf.getLinkId());
        wrapper.set((ObjectUtil.isNotNull(ecologyHatchRewardsConf.getLinkCount()) && NumberUtil.isGreater(ecologyHatchRewardsConf.getLinkCount(), BigDecimal.ZERO)), EcologyHatchRewardsConf::getLinkCount, ecologyHatchRewardsConf.getLinkCount().setScale(2, BigDecimal.ROUND_DOWN));
        wrapper.eq(EcologyHatchRewardsConf::getId, ecologyHatchRewardsConf.getId());
        return update(new EcologyHatchRewardsConf(), wrapper);
    }

    /**
     * 获取熔炼方式藏品关联表
     *
     * @param id
     * @param ecologyHatchId
     * @param methodId
     * @return
     */
    public EcologyHatchRewardsConf getByIdAndEcologyHatchId(Long id, Long ecologyHatchId, Long methodId) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getId, id);
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        wrapper.eq(EcologyHatchRewardsConf::getMethodId, methodId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public boolean delRewardsById(Long id) {
        return removeById(id);
    }

    public boolean delRewardsByEcologyHatchId(Long ecologyHatchId) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        return remove(wrapper);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids) {
        LambdaUpdateWrapper<EcologyHatchRewardsConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(EcologyHatchRewardsConf::getId, ids);
        return remove(wrapper);
    }

    /**
     * 获取熔炼奖励所对应的奖励
     *
     * @param ecologyHatchId
     * @param rewardsType
     * @return
     */
    public List<EcologyHatchRewardsConf> getList(Long ecologyHatchId, EcologyRewardsType rewardsType) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        if (Objects.nonNull(rewardsType)) {
            wrapper.eq(EcologyHatchRewardsConf::getRewardsType, rewardsType);
        }
        return list(wrapper);
    }

    public boolean updateMethodId(Long ecologyHatchId, Long methodId) {
        LambdaUpdateWrapper<EcologyHatchRewardsConf> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EcologyHatchRewardsConf::getMethodId, methodId);
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        return update(new EcologyHatchRewardsConf(), wrapper);
    }

    public EcologyHatchRewardsConf getByEcologyHatchId(Long ecologyHatchId, EcologyRewardsType rewardsType) {
        LambdaQueryWrapper<EcologyHatchRewardsConf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EcologyHatchRewardsConf::getEcologyHatchId, ecologyHatchId);
        wrapper.eq(EcologyHatchRewardsConf::getRewardsType, rewardsType);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }
}